<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>论坛主页</title>
    <link rel="stylesheet" href="{{ url_for('static', filename='css/navigation_bar.css') }}">
    <link rel="stylesheet" href="{{ url_for('static', filename='css/posts.css') }}">
</head>
<body>

    <header>
        <nav class="nav-container">
            <div class="nav-left">
                <a href="#" class="nav-brand">论坛管理信息系统</a>
            </div>
            <div class="nav-center">
                <form class="nav-search" action="{{ url_for('search') }}" method="post">
                    <input type="text" name="keyword" placeholder="搜索..." class="search-input">
                    <button type="submit" class="search-button">搜索</button>
                </form>
            </div>
            <div class="nav-right">
                <a href="#" class="nav-link">首页</a>
                <a href="{{ url_for('login') }}" class="nav-link">注销</a>
            </div>
        </nav>
    </header>

    <div class="主体">
        <div class="板块列表">
            <ol>
                {% for post in all_posts %}
                <div class="post-container">
                    <div class="post-header">
                        <h2 class="post-title">{{ post[1] }}</h2>
                        <div class="post-meta">
                            <div class="post-tags-container">
                                <span class="post-tags"><a href="{{ url_for('in_category', category = post[4]) }}">{{ post[4] }}</a></span>
                            </div>
                            <span class="post-author">帖主: <a href="{{ url_for('in_detail', user_id = post[6]) }}">{{ post[7] }}</a></span>
                            <span class="post-date">{{ post[2] }}</span>
                        </div>
                    </div>
                    <div class="post-content">
                        <p>{{ post[3] }}</p>
                    </div>
                    <div class="post-footer">
                        <a href="{{ url_for('admin_post',  post_id=post[0]) }}" class="post-details-link">查看详情>></a>
                        <button class="delete-button" onclick="deletePost({{ post[0] }})">删除</button>
                    </div>
                </div>
                {% endfor %}
            </ol>
        </div>
    </div>

</body>
</html>

<script>
    function deletePost(postId) {
        if (confirm("确定要删除这个帖子吗?")) {
            fetch(`/delete_post/${postId}`, {
                method: 'POST'
            })
            .then(response => {
                if (response.ok) {
                    window.location.reload();
                } else {
                    alert("删除帖子失败.");
                }
            })
            .catch(error => {
                console.error("删除帖子时出错:", error);
                alert("删除帖子时出现错误.");
            });
        }
    }
</script>